﻿// ## 2023/09/15 # 江东新风 # 初次编写 ##


/*
ai扩建功能----
扩建支出阶梯化----√
扩建城市影响城市人口上限----√
执行任务跨回合----
*/
namespace 都市指令_扩建
{

	// ========= CUSTOMIZE ==========

	//微->小，小->中，中->大，大->巨
	const array<int> TP_COST = {100,300,500,1000};
	const array<int> GOLD_COST = {800,1600,3200,5000};

	const int ACTION_COST = 40;  // 
		// ==============================

	class Main
	{

		pk::building@ building_;
		pk::force@ force_;

		pk::person@ actor_;
		Main()
		{
			add_menu();
			pk::bind2(102, pk::trigger102_t(callback));
			pk::bind(202, pk::trigger202_t(onAIRunningOrder));
		}

		void add_menu()
		{
			pk::menu_item 扩建;
			扩建.menu = 100;
			扩建.shortcut = "9";
			扩建.init = pk::building_menu_item_init_t(init);
			扩建.is_visible = pk::menu_item_is_visible_t(isVisible);
			扩建.is_enabled = pk::menu_item_is_enabled_t(isEnabled);
			扩建.get_text = pk::menu_item_get_text_t(getText);
			扩建.get_desc = pk::menu_item_get_desc_t(getDesc);
			扩建.handler = pk::menu_item_handler_t(handler);
			pk::add_menu_item(扩建);
		}

		//游戏初始化时设置城市初始级别
		void callback()
		{
			if (!pk::get_scenario().loaded)
			{
				//for (int i = 0; i < 城市_末; ++i)
				//{
				//	ch::get_base_p(i).city_level = ch::get_city_level(i);
				//	ch::set_interior_land(i);
				//	
				//}
			}
			else
			{
				for (int i = 0; i < 城市_末; ++i)
				{
					pk::update_building_model(pk::get_building(i));
				}
				
			}
		}

		void init(pk::building@ building)
		{
			@building_ = @building;
			@force_ = pk::get_force(building.get_force_id());
		}

		bool isVisible()
		{
			return !pk::is_campaign();
		}

		bool isEnabled()
		{
			auto person_list = pk::get_idle_person_list(building_);
			int result = check_avaliable(building_, person_list);
			if (result != 0) return false;
			else return true;
		}

		string getText()
		{
			return pk::encode("扩建");  //chujue
		}

		string getDesc()
		{
			auto base_t = ch::get_base_p(building_.get_id());
			uint8 level = base_t.city_level;
			auto person_list = pk::get_idle_person_list(building_);
			int result = check_avaliable(building_, person_list);
			switch (result)
			{
			case 1: return pk::encode("该城市已达最高级，无法继续扩建");
			case 2: return pk::encode("没有可执行的武将. ");
			case 3: return pk::encode(pk::format("行动力不足 (必须 {} 行动力)", ACTION_COST));
			case 4: return pk::encode(pk::format("资金不足 (必须 {} 资金)", GOLD_COST[level]));
			case 5: return pk::encode(pk::format("技巧值不足 (必须 {} 技巧)", TP_COST[level]));
			case 6: return pk::encode(pk::format("人口不足，无法扩建 (下级需要 {}万 人口)", ch::get_level_requred_pop(base_t.city_level+1)/10000));
			case 0: return pk::encode(pk::format("扩建都市(消耗：金{} 行动力{} 技巧{})", GOLD_COST[level], ACTION_COST, TP_COST[level]));
			default:
				return pk::encode("");
			}
			return pk::encode("");

		}

		int check_avaliable(pk::building@ building, pk::list<pk::person@> list)//之所以加入list是为了ai调用时不用重复计算，玩家菜单稍微多点操作问题不大
		{
			auto base_t = ch::get_base_p(building.get_id());
			uint8 level = base_t.city_level;
			if (level >= 4) return 1;
			else if (list.count == 0) return 2;
			else if (pk::get_district(building.get_district_id()).ap < ACTION_COST) return 3;
			else if (pk::get_gold(building) < GOLD_COST[level]) return 4;
			else if (pk::get_force(building.get_force_id()).tp < uint(TP_COST[level])) return 5;
			else if ((base_t.population +base_t.mil_pop_all) < ch::get_level_requred_pop(level + 1)) return 6;
			else return 0;
		}

		bool handler()
		{
			int taishu_id = pk::get_taishu_id(building_);
			if (!pk::is_valid_person_id(taishu_id)) taishu_id = 武将_文官;
			pk::person@ taishu_ = pk::get_person(taishu_id);
			pk::city@ city = pk::get_city(building_.get_id());
			string city_name = pk::decode(pk::get_name(city));

			auto base_t = ch::get_base_p(building_.get_id());
			uint8 level = base_t.city_level;

			if (pk::choose({ pk::encode(" 是 "), pk::encode(" 否 ") }, pk::encode(pk::format("是否在{}执行扩建？\n(消耗：金{} 行动力{} 技巧{})\n(提升城市人口上限，提升内政用地上限)", city_name, GOLD_COST[level], ACTION_COST, TP_COST[level])), taishu_) == 1)
				return false;

			pk::list<pk::person@> person_list = pk::get_idle_person_list(building_);
			if (person_list.count == 0) return false;

			// 실행무장 선택하기
			pk::list<pk::person@> person_sel;
			pk::list<pk::person@> pre_sel;
			person_list.sort(function(a, b)
			{
				return (a.stat[武将能力_政治] > b.stat[武将能力_政治]); // 무장 정렬 (지력순)
			});
			for (int i = 0; i < pk::min(3, person_list.count); ++i)
			{
				pre_sel.add(person_list[i]);
			}

			person_sel = pk::person_selector(pk::encode("武将选择"), pk::encode("选择执行扩建的武将."), person_list, 1, 3, pre_sel,1);
			if (person_sel.count == 0) return false; // 미선택 시 취소 종료


			return run_order(person_sel, building_);


		}

		void onAIRunningOrder(pk::ai_context@ context, pk::building@ building, int cmd)
		{
			if (cmd == 据点AI_扩建)
			{
				pk::list<pk::person@> person_sel;
				if (run_order_before(person_sel, building))
				{
					run_order(person_sel, building);
					ch::remove_idle_person(context, person_sel);
				}
				//else if (调试模式) pk::trace("据点AI_计略扰乱 不满足");
			}
		}


		bool run_order_before(pk::list<pk::person@>& out person_sel, pk::building@ building0)
		{
			pk::list<pk::person@> person_list = pk::get_idle_person_list(building0);
			if (person_list.count == 0) return false;
			person_list.sort(function(a, b)//武将选择这块还得加强
			{
				int stat_sum_a = 0;
				int stat_sum_b = 0;
				for (int i = 0; i < 武将能力_末; ++i)
				{
					stat_sum_a += a.stat[i];
					stat_sum_b += b.stat[i];
				}
				return (stat_sum_a < stat_sum_b); // 目前反正啥都不看，就选能力最低的

			});
			person_sel.add(person_list[0]);
			if (check_avaliable(building0, person_sel) != 0) return false;
			
			return true;
		}


		bool run_order(pk::list<pk::person@> person_sel, pk::building@ building0)
		{
			if (building0 is null) return false;
			if (person_sel[0] is null) return false;
		
			//做成跨回合太麻烦，以后再研究，需要扩充武将任务数量
			pk::force@force = pk::get_force(building0.get_force_id());
			int city_id = building0.get_id();
			for (int i = 0; i < person_sel.count; ++i)
			{
				person_sel[i].action_done = true;
				person_sel[i].kouseki += 100;
			}
			if (pk::is_player_controlled(building0))
			{
				//pk::city@ city = pk::get_city(city_id);
				//string city_name = pk::decode(pk::get_name(city));
				//pk::say(pk::encode(pk::format("成功扩建了{}。",city_name)), person_sel[0]);
				@actor_ = @person_sel[0];
				pk::scene(pk::scene_t(scen_扩建));


				//pk::message_box(pk::encode(pk::format("在\x1b[2x{}\x1b[0x等人的领导下，\x1b[1x{}军\x1b[0x对\x1b[1x{}\x1b[0x进行了扩建。", pk::decode(pk::get_name(person_sel[0])), pk::decode(pk::get_name(force)), city_name)));
			}
			
			
			auto base_t = ch::get_base_p(city_id);

			uint8 level = base_t.city_level;
			base_t.city_level += 1;
			ch::set_interior_land(city_id);
			pk::update_building_model(building0);

			auto district = pk::get_district(building0.get_district_id());
			pk::add_ap(district, -ACTION_COST);

			pk::add_gold(building0, -GOLD_COST[level], true);

			
			if (pk::is_alive(force))
				pk::add_tp(force, -TP_COST[level], building0.pos);
			//ch::get_base_p(building0.get_id()).perturb_done = true;

			string msg = pk::encode(pk::format("\x1b[1x{}\x1b[0x对\x1b[1x{}\x1b[0x进行了扩建，城池级别变为{}了！", pk::decode(pk::get_name(force)), pk::decode(pk::get_name(building0)), ch::get_level_desc(base_t.city_level, false)));
			pk::history_log(building0.pos, force.color, msg);

			return true;

		}

		void scen_扩建()
		{
			pk::play_bgm(17);

			pk::play_bgm(17);
			pk::fade(0);
			pk::sleep();
			pk::background(场景_建设);
			pk::play_bgm(17);
			pk::fade(255);
			pk::message_box(pk::encode("大家再加把劲吧！争取在这个月扩建完成！"), actor_);
			pk::message_box(pk::encode("好的！大家一起加油。"), pk::get_person(武将_劳动者));

			pk::fade(0);
			pk::background(-1);
			pk::fade(255);

			pk::force@ force = pk::get_force(actor_.get_force_id());
			pk::city@ city = pk::get_city(actor_.service);
			string city_name = pk::decode(pk::get_name(city));
			pk::message_box(pk::encode(pk::format("在\x1b[2x{}\x1b[0x等人的领导下，\x1b[1x{}军\x1b[0x对\x1b[1x{}\x1b[0x进行了扩建。", pk::decode(pk::get_name(actor_)), pk::decode(pk::get_name(force)), city_name)));

			pk::cutin(CG_气势);


		}
	}

	Main main;
}